home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / rayl120.zip / RAYLATHE.C < prev    next >
C/C++ Source or Header  |  1993-04-24  |  7KB  |  214 lines

  1. /*   RayLathe (c) Koehler
  2.    - Thick <0 = draw solid, =0 = move, >0 = draw hollow
  3.    Revision History:
  4.    03-24-93 1.00 KJK  New. Inspired by uLathe.
  5.    03-28-93 1.01 KJK  Attempting Vivid output for Doug Downs.
  6.    04-11-93 1.10 KJK  Releasable Vivid output version.
  7.               Noticed THICK in Vivid no supported! Must fix if possible.
  8.    04-11-93 1.11 KJK  Cured black speckles in POVRAY output.
  9.    04-14-93 1.12 KJK  Allow 0 length cones (rings) for Vivid.
  10.    04-15-93 1.13 KJK  Enable THICK for Vivid. Print line # of .DAT file.
  11.    04-24-93 1.20 KJK  Since they are necessary when thickness is used,
  12.               simulate cone/rings for POVRAY.
  13. */
  14.  
  15. #include <math.h>
  16. #include <stdio.h>
  17.  
  18. #define min(a,b) (a<b ? a : b)
  19. #define max(a,b) (a>b ? a : b)
  20.  
  21. #define VERSION 1.20
  22.  
  23. double intercept(double, double, double, double);
  24. void  lathe_cut_viv(double, double, double, double, double);
  25. void  lathe_cut_pov(double, double, double, double, double);
  26.  
  27. void main(argc,argv)
  28. int argc;
  29. char *argv[];
  30.    {
  31.    double oldx = 0.01, oldy = 0.01;
  32.    double x, y, thick;
  33.    double boundminx=10000000, boundmaxx=-10000000, boundmaxy=0;
  34.    double length, center, eye_distance;
  35.    char tracer;     /* p=POVRAY  v=Vivid */
  36.    int line=0;
  37.  
  38.    fprintf(stderr,"RayLathe v%2.2f (c) 1993 Koehler\n",VERSION);
  39.  
  40.    tracer = 'p';
  41.    if(argc > 1)   /* Look for Vivid option */
  42.        {
  43.        if ((strcmp(argv[1],"-v")==0) || (strcmp(argv[1],"-V")==0))
  44.        tracer = 'v';
  45.        if ((strcmp(argv[1],"?")==0) || (strcmp(argv[1],"-?")==0) || (strcmp(argv[1],"/?")==0))
  46.        {
  47.        fprintf(stderr,"    Usage: RAYLATHE [-V] <infile.dat >outfile.inc\n");
  48.        exit();
  49.        }
  50.        }
  51.    if (tracer == 'p')
  52.        fprintf(stderr,"    Generating POVRAY 1.0 object  (use -v for Vivid)\n");
  53.    if (tracer == 'v')
  54.        fprintf(stderr,"    Generating Vivid 2.0 object\n");
  55.  
  56.    if (tracer == 'p')
  57.        {
  58.        printf("// POVRAY 1.0 object created by RayLathe v%2.2f (c) 1993 Koehler\n",VERSION);
  59.        printf("// See suggested camera vectors at end of this file\n\n");
  60.        printf("#declare LatheWork =\n");
  61.        printf("   composite\n");
  62.        printf("      {\n");
  63.        }
  64.    if (tracer == 'v')
  65.        {
  66.        printf("// Vivid 2.0 object created by RayLathe v%2.2f (c) 1993 Koehler\n",VERSION);
  67.        printf("// See overall dimensions of object at end of this file\n");
  68.        /* printf("// See suggested camera vectors at end of this file\n"); */
  69.        printf("\n");
  70.        }
  71.    scanf("%lf %lf %lf", &x, &y, &thick);
  72.    line++;
  73.    do
  74.        {
  75.        printf("// Line %4d (%lf, %lf, %lf)\n", line, x, y, thick);
  76.        if (thick)
  77.        {
  78.        if (tracer == 'p')
  79.            lathe_cut_pov(oldx, oldy, x, y, thick);
  80.        if (tracer == 'v')
  81.            lathe_cut_viv(oldx, oldy, x, y, thick);
  82.        }
  83.        boundmaxx = max(boundmaxx, x);
  84.        boundminx = min(boundminx, x);
  85.        boundmaxy = max(boundmaxy, y);
  86.        oldx = x;
  87.        oldy = y;
  88.        scanf("%lf %lf %lf", &x, &y, &thick);
  89.        line++;
  90.        }
  91.    while (y >= 0);
  92.    if (tracer == 'v')
  93.        printf("\n// Min X Y Z = %f %f %f    Max X Y Z = %f %f %f\n",boundminx,-boundmaxy,-boundmaxy, boundmaxx, boundmaxy, boundmaxy);
  94.    if (tracer == 'p')
  95.        {
  96.        printf("      bounded_by\n");
  97.        printf("         {\n");
  98.        printf("         box {<%f %f %f>  <%f %f %f> }\n",boundminx,-boundmaxy,-boundmaxy, boundmaxx, boundmaxy, boundmaxy);
  99.        printf("         }\n");
  100.        printf("      }\n");
  101.        length = boundmaxx - boundminx;
  102.        center = length / 2 + boundminx;
  103.        printf("\n#declare Look_At = <%f 0 0>   // Center of object\n", center);
  104.        eye_distance = -max(fabs(length), boundmaxy*2.4);
  105.        printf("#declare Location = <%f 0 %f>    // Good camera position\n", center, eye_distance);
  106.        }
  107.    }
  108.  
  109. void lathe_cut_viv(x1, y1, x2, y2, thick)
  110.    double x1, y1, x2, y2, thick;
  111.    {
  112.    double minx, miny, minz, maxx, maxy, maxz, origin;
  113.  
  114.    /* cone { base 1 1 1 base_radius 4  apex 0 0 5 apex_radius 1 } */
  115.    printf("   cone { base %f 0.0 0.0  base_radius %f   ", x1, y1);
  116.    printf("apex %f 0.0 0.0  apex_radius %f }\n", x2, y2);
  117.    if (thick > 0)
  118.        {
  119.        printf("   cone { base %f 0.0 0.0  base_radius %f   ", x1, max(y1-thick,0));
  120.        printf("apex %f 0.0 0.0  apex_radius %f }\n", x2, max(y2-thick,0));
  121.        printf("   cone { base %f 0.0 0.0  base_radius %f   ", x1, max(y1,0));
  122.        printf("apex %f 0.0 0.0  apex_radius %f }\n", x1, max(y1-thick,0));
  123.        printf("   cone { base %f 0.0 0.0  base_radius %f   ", x2, max(y2,0));
  124.        printf("apex %f 0.0 0.0  apex_radius %f }\n", x2, max(y2-thick,0));
  125.        }
  126.    }
  127.  
  128. void lathe_cut_pov(x1, y1, x2, y2, thick)
  129.    double x1, y1, x2, y2, thick;
  130.    {
  131.    double minx, miny, minz, maxx, maxy, maxz, origin;
  132.  
  133.    if (x1 == x2)
  134.        if (thick)
  135.        x1 = x1 * 1.001;    /* Fudge a flat cone (ring) */
  136.        else
  137.        return;
  138.    minx = min(x1,x2);
  139.    maxx = max(x1,x2);
  140.    maxy = max(y1,y2);
  141.    miny = -maxy;
  142.    maxz = maxy;
  143.    minz = miny;
  144. /* printf("      object       // Line %4d (%lf, %lf, %lf)\n", line, x2, y2, thick); */
  145.    printf("      object\n");
  146.    printf("         {\n");
  147.    printf("         intersection\n");
  148.    printf("            {\n");
  149.    if (y1 == y2)
  150.        {
  151.        if (y1 > 0)
  152.        {
  153.        printf("            quadric {Cylinder_X  ");
  154.        printf("scale <1 %f %f> }\n", y1, y1);
  155.        }
  156.        }
  157.    else
  158.        {
  159.        if ((x1 == 0) || (x2 == 0))
  160.        origin = 0;
  161.        else
  162.        origin=intercept(x1, y1, x2, y2);
  163.        printf("            quadric {QCone_X  ");
  164.        if (x1 == 0)
  165.        printf("scale <%f %f %f>  ", fabs(origin-x2), y2, y2);
  166.        else
  167.        printf("scale <%f %f %f>  ", fabs(origin-x1), y1, y1);
  168.        printf("translate <%f 0 0> }\n", origin);
  169.        }
  170.    if ((thick >= 0) && (((y1-thick)>0) || ((y2-thick)>0)))
  171.        {
  172.        if (y1 == y2)
  173.        {
  174.        if ((y1-thick) > 0)
  175.            {
  176.            printf("            quadric {Cylinder_X  ");
  177.            printf("scale <1 %f %f> inverse }\n", y1-thick, y1-thick);
  178.            }
  179.        }
  180.        else
  181.        {
  182.        if ((x1 == 0) || (x2 == 0))
  183.            origin = 0;
  184.        else
  185.            origin = (((y1-thick)/y1)*(origin-x1))+x1;
  186.        printf("            quadric {QCone_X  ");
  187.        if ((origin-x1) == 0)
  188.            printf("scale <%f %f %f>  ", fabs(origin-x2), y2-thick, y2-thick);
  189.        else
  190.            printf("scale <%f %f %f>  ", fabs(origin-x1), y1-thick, y1-thick);
  191.        printf("translate <%f 0 0> inverse }\n", origin);
  192.        }
  193.        }
  194.    printf("            box {<%f %f %f>  <%f %f %f> }\n",minx,miny,minz,maxx,maxy,maxz);
  195.    printf("            }\n");
  196.    minx = minx*1.001;  /* Adjust for bounding */
  197.    maxx = maxx*1.001;
  198.    maxy = maxy*1.001;
  199.    miny = -maxy;
  200.    maxz = maxy;
  201.    minz = miny;
  202.    printf("         bounded_by\n");
  203.    printf("            { box {<%f %f %f>  <%f %f %f> } }\n",minx,miny,minz,maxx,maxy,maxz);
  204.    printf("         texture { LatheWorkTex }\n");
  205.    printf("         }\n");
  206.    }
  207.  
  208. double intercept(x1, y1, x2, y2)
  209.    double x1, y1, x2, y2;
  210.    {
  211.    double result;
  212.    return(x2 - ((x2-x1) / (y2-y1) * y2));
  213.    }
  214.